package com.ysb.searchserver;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;

public class LastIdSaver {
	
	public final String savepath;
	
	public LastIdSaver(String savepath) {
		this.savepath = savepath;
	}
	
	public void save(int value) {
		FileOutputStream fos = null;
		try {
			fos = new FileOutputStream(new File(this.savepath));
			ByteBuffer bb = ByteBuffer.allocate(4);
			bb.putInt(value);
			bb.flip();
			fos.write(bb.array());
		}
		catch(Throwable th){
			
		}
		finally {
			if(fos != null) {
				try {
					fos.close();
				}
				catch(Throwable th) {}
			}
		}
		
	}
	
	public int get(int def) {
		FileInputStream fis = null;
		try {
			fis = new FileInputStream(new File(this.savepath));
			byte[] bs = new byte[4];
			int len = 0;
			while((len = fis.read(bs, len, 4-len))>=0);
			ByteBuffer bb = ByteBuffer.wrap(bs);bb.flip();
			return bb.getInt();
		}
		catch(Throwable th) {
			return def;
		}
		finally {
			if(fis != null) {
				try {
					fis.close();
				}
				catch(Throwable th) {}
			}
		}
	}
}
